class Solution {
    public int mySqrt(int x) {
        long left = 1, right = x;
        long mid = 0;
        if (x == 0 || x == 1) {
            return x;
        }
        while (left < right) {
            mid = left + (right - left + 1) / 2;
            if (mid * mid <= x) {
                left = mid;
            } else if (mid * mid > right) {
                right = mid - 1;
            }
        }
        return (int) left;
    }
}